#ifndef ClientSideSynchronizer_H_
#define ClientSideSynchronizer_H_

#include <string>
#include "MyMap.h"
#include "SnakePlayer.h"
#include "SnakeSegment.h"
#include "PlayerMoves.h"
#include "PlayerSynchronizer.h"
#include "udp-client/UdpClient.h"

/** \brief ClientSideSynchronizer - Singleton*/
class ClientSideSynchronizer : public PlayerSynchronizer
{
private:
	static ClientSideSynchronizer * instance;
	ClientSideSynchronizer();
	UdpClient c;
	bool connected;
	int player_number;
	Direction last_direction;
	bool already_in_game;
	int local_round_number;
public:
	static ClientSideSynchronizer * getInstance();
	static PlayerMoves * deserializePlayerMoves(std::string & dataString);
	void putPlayerOnTheMap(PlayerMoves * moves);
	void synchronize();
	virtual ~ClientSideSynchronizer();
	void waitToStart();
	bool connectToServer();
	int getPlayerWinLostStatus(int number);
	void startThreads();
	void sendLastDirection();
	void resolvePlayerData();
	void mergePositions();
	void rollbackSnakeAndRecover(int i);
public:
	//inline
	void setPlayerNumber(int number)
	{
		player_number = number;
	}
	int getPlayerNumber()
	{
		return player_number;
	}
	void setLastDirection(Direction d)
	{
		last_direction = d;
	}
	Direction & getLastDirection()
	{
		return last_direction;
	}
	UdpClient & getUdpClient()
	{
		return c;
	}
	void incLocalRound()
	{
		local_round_number++;
	}
	int getLocalRound()
	{
		return local_round_number;
	}
};

void *client_game_func(void *ptr);
void *client_resolver_func(void *ptr);
void *client_sender_func(void *ptr);

#endif /*ClientSideSynchronizer_H_*/
